<script type="text/javascript">
var s_lm_receivemode = true;
var s_lm_reqid = -1;
var s_lm_row = 0;
var s_lm_uid = 0;
var only_see_him_mode;
$(document).ready(function(){
	   var data = JSON.parse($(".s_temple_setting.list_leave_message").html());
	   s_lm_receivemode=data.receivemode;
	   only_see_him_mode = data.only_see_him_mode;
	   if(data.receivemode){
			$(".s_lm_receive_mode").addClass("active");
		}else{
			$(".s_lm_sent_mode").addClass("active");
		}
	   s_lm_reqid = data.reqid;
	   s_lm_uid = data.uid;
	   loadAjaxEntry();
		$(".s_lm_load_more").click();
});

function s_lm_loadMore() {
        if (s_lm_receivemode) ajaxRequest("offset=" + s_lm_row + "&sender=" + s_lm_reqid, "index.php?mod=home&childmod=notice&ajax=true&op=receive", s_lm_onSuccess, s_lm_onError);
        else ajaxRequest("offset=" + s_lm_row + "&target=" + s_lm_reqid, "index.php?mod=home&childmod=notice&ajax=true&op=sent", s_lm_onSuccess, s_lm_onError);
    
}
function s_lm_onSuccess(data){
	console.log(data);
	s_lm_operatedone(data.code,data.message,data.data);
}
function s_lm_onError(data){
	console.log(data);
	s_lm_operatedone(-101,"服务器返回了未知的数据，请前往console查看",data);
}
function s_lm_load_list(data){
	var $container = $("<div class='ui feed' id='offset-"+s_n_row+"'>")
	$.each(data,function(k,v){
		$container.prepend(s_lm_loadLeaveMessage(v));
	});
	$("#leavemessages").append($container);
}
function s_lm_loadLeaveMessage(data){
	var $tmp = $("#leavemessage_temple").clone();
	$tmp.attr("id","").attr("style","");
	$tmp.find(".s_uid").attr("uid",data.uid);
	$tmp.find(".s_target").attr("target",data.target);
	$tmp.find(".s_lid").attr("lid",data.lid);
	if(only_see_him_mode){
		$tmp.find(".s_lm_only_read_him").hide();
	}
	if(s_lm_receivemode){
		$tmp.find(".s_leavemessage_uid_href").attr("href",getHomeURL(data.uid));
		$tmp.find(".s_leavemessage_name").html(data.ucard.name);
		$tmp.find(".s_leavemessage_header").attr("src",data.ucard.head_image_path);
		if(data.is_read==0){
			$tmp.find(".s_lm_readstate").html("未读消息");
		}else{
			$tmp.find(".s_lm_readstate").html("已读消息");
		}
	}else{
		$tmp.find(".s_leavemessage_uid_href").attr("href",getHomeURL(data.target));
		$tmp.find(".s_leavemessage_name").html(data.tcard.name);
		$tmp.find(".s_leavemessage_header").attr("src",data.tcard.head_image_path);
		$tmp.find(".s_lm_reply").hide();
		if(data.is_read==0){
			$tmp.find(".s_lm_readstate").html("对方未读");
		}else{
			$tmp.find(".s_lm_readstate").html("对方已读");
		}
	}
	if(data.is_read==0){
		$tmp.find(".s_leavemessage_text").html("<b>"+new Base64().decode(data.content)+"</b>");
	}else{
		$tmp.find(".s_leavemessage_text").html(new Base64().decode(data.content));
		}
	$tmp.find(".s_leavemessage_date").html(data.date);
	return $tmp;
}
function s_lm_operatedone(id,message,data){
	if(id==-101){
		showMessage("s_lm_message",-101,data.responseText);
		return;
	}
	if(data.op=="load_more"){
		if(data.bottom){
			$(".s_lm_load_more").html("到底了");
			$(".s_lm_load_more").addClass("disabled");
		}
		s_lm_load_list(data.data);
		console.log(data.amount);
		s_lm_row +=data.amount;
	}
}
function s_lm_replyLM(s){
	var uid = s.attr("uid");
	var lid = s.attr("lid");
		$(".s_ajax_container").load("index.php?mod=home&childmod=notice&ajax=true&op=sendleavemessage&luid="+uid+"&lreply="+lid);
}
function s_lm_only_see_him(s){
	if(s_lm_receivemode){
			var uid = s.attr("uid");
		$(".s_ajax_container").load("index.php?mod=home&childmod=notice&ajax=true&op=receive_q&luid="+uid);
	}else{
		var target = s.attr("target"); 
		$(".s_ajax_container").load("index.php?mod=home&childmod=notice&ajax=true&op=sent_q&luid="+target);
	}
}
</script>
<h2 class="s_lm_leaveList">留言列表</h2>
<div class="s_temple_setting list_leave_message"><?php print_json_setting("list_leave_message");?></div>
<div class="ui two item menu s_l_a_topbar">
	<a class="item s_ajax_entry s_lm_receive_mode" url="index.php?mod=home&childmod=notice&ajax=true&op=receive_q" targetid="s_ajax_container">我收到的</a>
	<a class="item s_ajax_entry s_lm_sent_mode" url="index.php?mod=home&childmod=notice&ajax=true&op=sent_q" targetid="s_ajax_container">我发出的</a>
</div>
<div class="ui message hidden" id="s_lm_message"></div>
<div class="ui feed" id="leavemessages"></div>
<button class="ui fluid button s_lm_load_more" onClick="if($(this).hasClass('disabled'))return; s_lm_loadMore()">加载更多</button>
<div class="event" style="display: none" id="leavemessage_temple">
	<div class="label">
		<img class="s_leavemessage_header">
	</div>
	<div class="content">
		<div class="summary">
			<a class="s_leavemessage_uid_href">
				<span class="s_leavemessage_name s_activity_name"></span>
			</a>
			<a class="s_lm_reply lm_item s_lid s_uid" onClick="s_lm_replyLM($(this));"> 回复 </a>
			<a class="lm_item s_lm_only_read_him s_uid s_target" onClick="s_lm_only_see_him($(this))"> 只看TA的 </a>
			<div class="lm_item s_lm_readstate">已读</div>
			<div class="date s_leavemessage_date"></div>
		</div>
		<div class="extra text s_leavemessage_text"></div>
	</div>
</div>